
#************************************************
# telnet login routing with a command
#
# @PARAMS
# host	- target device ip
# user	- login user
# passwd - user's password
# cmd 	- the command list which device need to be executed
# @RETURN
# 0 - encount an eof
# 2 - telnet timeout
#************************************************
proc telnet_login_cmd {host user passwd cmd} {
	set timeout 10
	spawn telnet $host
	expect_after eof { exit 0 }
	expect {
		-nocase "login:"	{send "$user\r";exp_continue}
		-nocase "Password:"	{send "$passwd\r"}
		timeout    	{ exit 2}
	}
	send "$value\r"
	expect "*#"
	send "exit\r"	
	expect eof
}


#************************************************
# telnet login routing with command list
#
# @PARAMS
# host	- target device ip
# user	- login user
# passwd - user's password
# cmd 	- the command list which device 
#	   need to be executed
# @RETURN
# 0 - encount an eof
# 2 - telnet timeout
#************************************************
proc telnet_login_cmd_list {host user passwd cmds} {
	set timeout 10
	spawn telnet $host
	expect_after eof { exit 0 }
	expect {
		-nocase "login:"	{send "$user\r";exp_continue}
		-nocase "Password:"	{send "$passwd\r"}
		timeout    	{ exit 2}
	}

	foreach value $cmds {
		send "$value\r"
		expect "*#"
	}
	send "exit\r"
	expect eof
}

#************************************************
# ssh login routing with a command
#
# @PARAMS
# host	- target device ip
# user	- login user
# passwd - user's password
# cmd 	- the command which device need to be executed
# @RETURN
# 0 - encount an eof
# 3 - no route to device
#************************************************
proc ssh_login_cmd {host user passwd cmd} {
	set timeout 10
	spawn ssh -p 22 $user@$host
	expect_after eof { exit 0 }
	expect {
		"want"		{send "yes\r"; exp_continue}
		"password:"	{send "$passwd\r"}
		"No route"	{exit 3 }
	}
	
	send "$value\r"
	expect "*#"

	send "exit\r"
	expect eof
}

#************************************************
# ssh login routing with command list
#
# @PARAMS
# host	- target device ip
# user	- login user
# passwd - user's password
# cmd 	- the command list which device 
#	  need to be executed
# @RETURN
# 0 - encount an eof
# 3 - no route to device
#************************************************
proc ssh_login_cmd_list {host user passwd cmds} {
	set timeout 10
	spawn ssh -p 22 $user@$host
	append logfile $host _expect.log
	log_file $logfile
	expect_after eof { exit 0 }
	expect {
		"want"		{send "yes\r"; exp_continue}
		"password:"	{send "$passwd\r"}
		"No route"	{exit 3 }
	}
	
	foreach value $cmds {
		send "$value\r"
		expect "*#"
	}
	send "exit\r"
	expect eof
}

#************************************************
# debug output routine
#
# @PARAMS
# arg - variable length arguments
#************************************************
proc debug arg {
    global g_debug_flag

    if {$g_debug_flag} {
        puts $arg
    }
}

#************************************************
# error output routine
#
# @PARAMS
# arg - variable length arguments
#************************************************
proc error_log arg {
    global g_log_flag
    global g_debug_flag

    if {$g_debug_flag} {
        puts $arg
    }
    if { $g_log_flag != 0 } {
        puts $g_log_flag $arg
    }
}

#************************************************
# get a line from file, skip blank lines and
# comment lines, return the reading line in
# parameter 'line'.
#
# @PARAMS
# fd     - file fd
# line   - var used to return the line
#
# @RETURN
# return 1 if read successfully, otherwise 0
#************************************************
proc get_line {fd line log_fd} {
    upvar $line ln

    # read a line from fd
    while {[set line_len [gets $fd ln]] >= 0} {
        # blank line
        if { $line_len == 0 } continue
 
        # trim whitespace
        set ln [string trim $ln]
        if { [string length $ln] == 0 } continue

        # skip comment
        if { [string index $ln 0] == "#" } continue
		puts $log_fd "\t\t<cmd>$ln</cmd>"	
        # success
        return 1
    }
    return 0
}

#************************************************
# This proc will extra lines from cmd_file into 
# a list.
#
# @PARAMS
# cmd_file      - command file
#
# @RETURN
# -1		- command file does not exist
# -2		- login failed
#************************************************
proc exec_cmd_file_with_ssh {cmd_file log_fd} {
	global g_debug_flag
	global g_devip g_user g_passwd

	if [catch {open $cmd_file {RDONLY}} cmd_fd] {
		puts "cannot open $cmd_file  {RDONLY} !"
		exit 0
	}
	debug "open $cmd_file OK"
	set cmd_list {}
	while true {
		# get a line
		if {![get_line $cmd_fd line $log_fd]} {
			debug "reached eof"
			break
		}
		lappend cmd_list $line
	}
	ssh_login_cmd_list $g_devip $g_user $g_passwd $cmd_list
	close $cmd_fd
}
#************************************************
# This proc will extra lines from cmd_file into 
# a list.
#
# @PARAMS
# cmd_file      - command file
#
# @RETURN
# -1		- command file does not exist
# -2		- login failed
#************************************************
proc exec_cmd_file_with_telnet {cmd_file} {
	global g_debug_flag
	global g_devip g_user g_passwd

	if [catch {open $cmd_file {RDONLY}} cmd_fd] {
		puts "cannot open $cmd_file  {RDONLY} !"
		exit 0
	}
	set cmd_list {}
	while true {
		# get a line
		if {![get_line $cmd_fd line]} {
			debug "$cmd_file reached eof"
			break
		}
		lappend cmd_list $line
	}
	telnet_login $g_devip $g_user $g_passwd $cmd_list
	close $cmd_fd
}

#************************************************
# print the shell environnement variables
#
# @PARAMS
# args      - environnement variables
#
#************************************************
proc printenv {args} {
	global env
	set maxl 0
	if {[llength $args] == 0} {
		set args [lsort [array names env]]
	}
	foreach x $args {
		if {[string length $x] >  $maxl} {
			set maxl [string length $x]
		}
	}
	incr maxl 2
	foreach x $args {
		puts stdout $env($x)
	}
}
